package ch.zbw.pra.sysventory.application.controller;

import java.io.Serializable;
import java.util.List;

import ch.zbw.pra.sysventory.business.businessObject.user.UserBusinessObject;
import ch.zbw.pra.sysventory.model.User;

/**
 * Steuert was angezeigt werden soll, wenn sich ein User anmeldet
 * 
 * @author Ralph Koster
 */

public class UserController implements Serializable {

	private static final long serialVersionUID = 8368634933304356188L;
	
	private UserBusinessObject userBusinessObject;
	private static UserController userController;
	private User currentUser;
	private boolean isLoggedIn;

	public UserController() {
		this.userBusinessObject = new UserBusinessObject();
		this.isLoggedIn = false;
	}
	
	public static UserController getInstance() {
		if (userController == null) {
			userController = new UserController();
		}
		return userController;
	}

	public boolean checkLogin(String username, String password) {
		List<User> userlist = userBusinessObject.getAllUsersFromDataAccess();
		for (User user : userlist) {
			if (username.equals(user.getUsername())
					&& password.equals(user.getPassword())) {
				currentUser = user;
				isLoggedIn = true;
			}
		}
		return isLoggedIn();
	}

	public int getUserRole() {
		return currentUser.getRole();
	}

	public boolean isLoggedIn() {
		return isLoggedIn;
	}
	
	public void setIsLoggedIn(boolean isLoggedIn) {
		this.isLoggedIn = isLoggedIn; 
	}
	
	public boolean hasPermission() {
		boolean bool = false;
		if(currentUser != null && currentUser.getRole() == 1) {
			bool = true;
		}
		return bool;
	}
}
